package com.cjc.mylibrary.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjc.mylibrary.R;
import com.cjc.mylibrary.entity.Menu;
import com.cjc.mylibrary.entity.SubMenu;

/**
 * menuAdapter
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>menuAdapter</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2014-8-21 下午2:53:57</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>菜单Adapter</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class LeftListAdpter extends BaseAdapter {
	private Context context;
	private List<Menu> Gmenu;
	private List<SubMenu> Smenu;
	private LayoutInflater mInflater;
	private int i;
	private List<String> sub;

	public LeftListAdpter(Context context, List<Menu> Gmenu, List<SubMenu> smenu) {
		mInflater = LayoutInflater.from(context);
		this.Gmenu = Gmenu;
		this.Smenu = smenu;
		this.context = context;
	}

	@Override
	public int getCount() {
		return Gmenu.size();
	}

	@Override
	public Object getItem(int arg0) {
		return Gmenu.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public List<String> getSubMenuSubValue(int position) {
		List<String> sub = new ArrayList<String>();
		String MenuNo = Gmenu.get(position).MenuNO;
		int valuecount = Smenu.size();
		for (int i = 0; i < valuecount; i++) {
			if (MenuNo.equals(Smenu.get(i).MenuNO)) {
				sub.add(Smenu.get(i).SubValue);
			}
		}
		return sub;
	}

	public List<String> getSubMenuSubName(int position) {
		List<String> sub = new ArrayList<String>();
		String MenuNo = Gmenu.get(position).MenuNO;
		int valuecount = Smenu.size();
		for (int i = 0; i < valuecount; i++) {
			if (MenuNo.equals(Smenu.get(i).MenuNO)) {
				sub.add(Smenu.get(i).SubName);
			}
		}
		return sub;
	}

	class ViewHolder {
		TextView GroupTV;
		LinearLayout footer;
		ImageView GmenuImage;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		convertView = mInflater.inflate(R.layout.expand_item, null);
		holder = new ViewHolder();
		holder.GmenuImage = (ImageView) convertView.findViewById(R.id.GmenuImage);
		// 通过GmenuImage设置菜单图标

		holder.GroupTV = (TextView) convertView.findViewById(R.id.GroupTV);
		holder.footer = (LinearLayout) convertView.findViewById(R.id.footer);
		holder.GroupTV.setText(Gmenu.get(position).Value);
		/* 设置菜单图标 */
		setImageIco(position, holder);
		sub = getSubMenuSubValue(position);
		System.out.println("看下有没有值 ：" + sub.size());
		if (!"0".equals(Gmenu.get(position).MenuType)) {
			/* 设置子菜单 */
			setSmenu(position, holder);
		}
		holder.footer.setVisibility(View.GONE);
		return convertView;
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 设置子菜单
	 * 
	 * @param position
	 * @param holder
	 ***************************************************** 
	 */
	private void setSmenu(int position, ViewHolder holder) {
		int count = sub.size();
		for (i = 0; i < count; i++) {
			LinearLayout smenuLinearLayout = new LinearLayout(context);
			smenuLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			TextView textView = new TextView(context);
			textView.setId(position + i);
			String string = sub.get(i);
			textView.setText(string);
			textView.setPadding(50, 15, 0, 15);
			textView.setTextSize(12);
			textView.setTextColor(Color.WHITE);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			smenuLinearLayout.addView(textView);
			View view = new View(context);
			view.setBackgroundColor(0xff898886);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
			holder.footer.addView(view);
			holder.footer.addView(smenuLinearLayout);
		}
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 设置菜单图标
	 * 
	 * @param position
	 * @param holder
	 ***************************************************** 
	 */
	private void setImageIco(int position, ViewHolder holder) {
		switch (Integer.valueOf(Gmenu.get(position).MenuNO)) {
		case 1:// ESSILOR NEWS
			holder.GmenuImage.setImageResource(R.drawable.muneicon_03);
			break;
		case 2:// SPEAKERS
			holder.GmenuImage.setImageResource(R.drawable.muneicon_06);
			break;
		case 3:// PROGRAMME
			holder.GmenuImage.setImageResource(R.drawable.muneicon_08);
			break;
		case 4:// FLOORPLAN
			holder.GmenuImage.setImageResource(R.drawable.muneicon_10);
			break;
		case 5:// DELEGATES OF THE WORLD
			holder.GmenuImage.setImageResource(R.drawable.muneicon_17);
			break;
		case 6:// ESSILOR ALL YOUR LIFE
			holder.GmenuImage.setImageResource(R.drawable.muneicon_19);
			break;
		case 7:// CONFERENCE SLIDER DOWNLOAD
			holder.GmenuImage.setImageResource(R.drawable.muneicon_21);
			break;
		case 8:// SURVEY
			holder.GmenuImage.setImageResource(R.drawable.muneicon_23);
			break;
		case 9:// HOME
			holder.GmenuImage.setImageResource(R.drawable.muneicon_home_03);
			break;
		default:
			holder.GmenuImage.setImageResource(R.drawable.muneicon_08);
			break;
		}
	}

}
